<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/11/18
 * Time: 21:40
 */

namespace app\storage\controller;

use app\storage\common\AppConstant;
use app\storage\model\BomDetailModel;
use app\storage\model\BomModel;
use app\storage\model\MaterielModel;
use app\storage\model\StorageModel;
use app\storage\service\bom\BaseBomService;
use app\storage\service\bom\BomFactory;
use app\storage\service\BomService;
use app\storage\service\CarService;
use cmf\controller\AdminBaseController;
use think\Request;


class BomController extends AdminBaseController
{

    private $bomModel;
    private $bomService;
    use \app\storage\util\Common;

    public function __construct(BomModel $bomModel, BomService $bomService, Request $request = null)
    {
        parent::__construct($request);
        $this->bomModel = $bomModel;
        $this->bomService = $bomService;
    }

    /**
     * 出库单主页面
     * @return mixed
     */
    public function index()
    {
        $assign = $this->getAssignStorage();
        $this->assign($assign);
        return $this->fetch('index');
    }

    /**
     * 出库单导入页面
     * @return mixed
     */
    public function add()
    {
        return $this->fetch('add');
    }

    /**
     * excel导入页面
     * @return mixed
     */
    public function excel()
    {
        return $this->fetch('excel');
    }

    /**
     * 批次退料单页面
     */
    public function batch()
    {
        $storageList = StorageModel::getStorageList();
        $this->assign('storage', $storageList);
        return $this->fetch('batch');
    }

    /**
     * 滞留物料页面
     */
    public function retention()
    {
        $storageList = StorageModel::getStorageList();
        $this->assign('storage', $storageList);
        return $this->fetch('retention');
    }

    /**
     *小车清单页面
     */
    public function car()
    {
        $assign['bomUuid'] = input('bomUuid');
        $this->assign($assign);
        return $this->fetch('car');
    }


    /**
     *小车清单表头
     */
    public function getListTitle()
    {
        $bomUuid = input('bomUuid');
        $carService = new CarService();
        $result = $carService->getCarListTitle($bomUuid);
        return json($result);
    }

    /**
     *小车清单内容
     */
    public function getCarList(){
        $bomUuid = input('bomUuid');
        $carService = new CarService();
        $result = $carService->getCarList($bomUuid);
        return json($result);
    }

    /**
     * 获取出库单主页面数据
     */
    public function getBom()
    {
        $where = $this->getWhere();
        $hidden = input('hidden');
        $where['where']['hidden'] = ['in', [$hidden, '2']];
        $result = $this->bomModel->getBom($where);
        return json($result);
    }

    /**
     * 获取单据详情
     */
    public function getBomDetail()
    {
        $storageUuid = input('storageUuid');
        $where['bom_uuid'] = input('bomUuid');
        $where['fentryid'] = input('fentryid');
        $where['order_id'] = input('orderId');
        empty($storageUuid) ? '' : $where['storage_uuid'] = $storageUuid;
        $bomDetailModel = new BomDetailModel();
        $result = $bomDetailModel->getBomDetail($where);
        return json($result);
    }

    /**
     * 删除订单
     */
    public function deleteBom()
    {
        $uuid = input('bomUuid');
        $result = $this->bomModel->deleteBom($uuid);
        return json($result);
    }

    /**
     * 将bom单的状态置为完成
     */
    public function setting()
    {
        $status['status'] = AppConstant::FINISH;
        $where['uuid'] = input('bomUuid');
        $res = $this->bomModel->where($where)->update($status);
        if ($res !== false) {
            $result = ret_template(true, '标记成功！');
        } else {
            $result = ret_template(false, '标记失败！');
        }
        return json($result);
    }

    /**
     * 重推该订单站位灯
     */
    public function rePush()
    {
        $bomUuid = input('bomUuid');
        $orderId = input('orderId');
        $result = $this->bomService->rePush($bomUuid, $orderId);
        return json($result);
    }


    /**
     * 将异常情况下的出库单结算
     */
    public function balance()
    {
        $bomUuid = input('bomUuid');
        $orderId = input('orderId');
        $result = $this->bomService->balance($bomUuid, $orderId);
        return json($result);
    }


    /**
     * 从K3导入新订单
     */
    public function addBomFromK3()
    {
        $hidden = input('hidden');
        $orderId = input('orderId');
        $result = $this->bomService->addBomFromK3($orderId, $hidden);
        return json($result);
    }

    /**
     * 从excel导入新订单
     */
    public function addBomFromExcel()
    {
        $result = $this->bomService->addBomFromExcel();
        return json($result);
    }

    public function findReels()
    {
        $param = input('');
        $type = $param['type'];
        $bomService = BomFactory::getBomService($type);
        $result = $bomService->findReels($param);
        return json($result);
    }

    /**
     * 创建滞、批次单据
     * @return \think\response\Json
     */
    public function createBom()
    {
        $reels = json_decode($_POST['reels'] ,true);
        $storageUuid = input('storageUuid');
        $baseBomService = new BaseBomService();
        $result = $baseBomService->createBom($reels, $storageUuid);
        return json($result);
    }

    /**
     * 单据excel下载
     * @return \think\response\Json
     */
    public function downloadExcel()
    {
        $type = input('type');
        $storageUuid = input('storageUuid');
        $reels = json_decode($_POST['reels'] ,true);
        $bomService = BomFactory::getBomService($type);
        $result = $bomService->downloadExcel($reels,$storageUuid);
        return json($result);
    }

}